//This test includes below steps:
//1 Open www.westpac.com.au
//2 Open home loan calculator in a new window
//3 Input parameters and calculate how much the customer can borrow
//4 Take a screenshot 

package practice;

import java.io.File;
import java.util.Iterator;

import org.apache.commons.io.FileUtils;
import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;



public class WestpacLoan {
	private WebDriver driver;
	
	private String baseUrl;
	private StringBuffer verificationErrors = new StringBuffer();
	@Before
	public void setUp() throws Exception {
		driver = new ChromeDriver();
		baseUrl = "http://www.westpac.com.au/";
		driver.get(baseUrl);
		
	}

	@Test
	public void testWestpacLoan() throws Exception {
		driver.findElement(By.linkText("Home loans")).click();
		driver.findElement(By.xpath("//a[@href='http://hlc1.westpac.com.au/hlc/hlc/BorrowingPowerStart.do']")).click();
		//Operate in the pop up window - switch focus onto the pop up window.
		String parentWindowHandle = driver.getWindowHandle(); // save the current window handle.
	    WebDriver popup = null;
	    Iterator<String> windowIterator = driver.getWindowHandles().iterator();
	    while(windowIterator.hasNext()) { 
	    	String windowHandle = windowIterator.next(); 
	        popup = driver.switchTo().window(windowHandle);
	      }
		//Operations in the pop up window
	     System.out.println(driver.getTitle());
	     new Select(driver.findElement(By.name("applicants"))).selectByVisibleText("Myself and another borrower");
		 	driver.findElement(By.name("Continue")).click();
			driver.findElement(By.name("income1")).clear();
			driver.findElement(By.name("income1")).sendKeys("10000");
			driver.findElement(By.name("income2")).clear();
			driver.findElement(By.name("income2")).sendKeys("1000");
			new Select(driver.findElement(By.id("fieldMediumSelect"))).selectByVisibleText("Married/defacto");
			new Select(driver.findElement(By.name("maritalStatus2"))).selectByVisibleText("Married/defacto");
			new Select(driver.findElement(By.id("fieldSmallSelect"))).selectByVisibleText("2");
			driver.findElement(By.name("calculate")).click();
			
			//Take screenshot
			File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
			// Now you can do whatever you need to do with it, for example copy somewhere
			FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot_westpac.png"));
			
			//Close pop up window
			driver.findElement(By.linkText("Close window")).click();
			
			
		
			//Switch focus back to the parent window.
			driver.switchTo().window(parentWindowHandle);
			System.out.println(driver.getTitle());
		    driver.switchTo().window(parentWindowHandle).close(); // Switch back to parent window.
	}

	@After
	public void tearDown() throws Exception {
		//driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}

	private boolean isElementPresent(By by) {
		try {
			driver.findElement(by);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}
}

